import {Message} from '@arco-design/web-vue';
import axios from "axios";
import {getRequest} from "@/utils/Request/NewApiRequest";

/**
 * 下载图片或资源
 * @param guid 图片标识
 * @constructor
 */
export const DownLoadPic = async (guid,filename) => {
    if (guid) {
        // 发送GET请求来下载图片
        const response = await axios.get(window.ip + `/API/GetFile/downloadFile?guid=${guid}`, {
            responseType: 'blob', // 设置响应类型为二进制流
        });
        // 创建一个Blob对象并生成一个下载链接
        const blob = new Blob([response.data]);
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = filename; // 设置下载文件的名称
        a.click();
        // 释放URL对象
        window.URL.revokeObjectURL(url);
        Message.success({
            id: 'messagesuccess',
            resetOnHover: true,
            content: `下载成功！`,
            duration: 2000
        })
    } else {
        Message.error({
            id: 'messageerror',
            resetOnHover: true,
            content: `请选择一条上传记录！`,
            duration: 2000
        })
    }
}

/**
 * 删除图片或者资源
 * @param guid 图片会资源的标识
 * @constructor
 */
export const DeletePic = (guid) => {
    return new Promise((resolve, reject)=>{
        getRequest(`GetFile/DeleteFile?guid=${guid}`).then(data=>{
            Message.success({
                id: 'messagesuccess',
                resetOnHover: true,
                content: `删除成功`,
                duration: 2000
            })
            resolve("删除成功")
        }).catch(err=>{
            Message.error({
                id: 'messageerror',
                resetOnHover: true,
                content: err,
                duration: 2000
            })
            reject(err)
        })
    })
}
